﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FAST
{
    /// <summary>
    ///  A wrapper class to access the FileSystem class.
    /// </summary>
    public class UpdateDetection
    {
        private FileSystem fs;
        private OriginalState leftOS, rightOS;
        private FolderPairHandler fph;
        private string leftOSPath, rightOSPath;

        public UpdateDetection(FolderPairHandler fph)
        {
            fs = new FileSystem();

            this.fph = fph;

            FolderPairItem fp = fph.getLastLoadedFolderPair();
            if (fp != null)
            {
                leftOSPath = fp.getNewOriginalStatePathLeft();
                rightOSPath = fp.getNewOriginalStatePathRight();
            }
            else
            {
                leftOSPath = "";
                rightOSPath = "";
            }
            leftOS = new OriginalState(leftOSPath); //use default file to write path
            rightOS = new OriginalState(rightOSPath); //use default file to write path
            fs.setOSFileDetails(leftOS.getOriginalState(), rightOS.getOriginalState());
        }

        private void initializeFolderPairs()//refactor
        {

            string path = Application.StartupPath;
            string diskLetter = path.Substring(0, path.IndexOf('\\') + 1);
            string storagePath = diskLetter + @"\sessions.dat";//refactor
            Serializer ser = new Serializer();
            if (File.Exists(storagePath))
                fph = ser.deserializeFolderPairHandler(storagePath);
            else
            {
                fph = new FolderPairHandler();
            }
        }

        public void removeOS()
        {
            fs.setOSFileDetails(null, null);
        }

        public List<FilesHandler> getLeftFolderList()
        {
            return fs.getLeftFileDetails();
        }

        public List<FilesHandler> getRightFolderList()
        {
            return fs.getRightFileDetails();
        }

        //recursively generate left folder tree structure
        public void processLeftFolder(string src)
        {
            fs.generateLeftFolderTree(src, src);
        }

        //recursively generate right folder tree structure and compare each file in right with all files in left
        public void processRightFolder(string des)
        {
            fs.generateRightFolderTree(des, des);
        }

    }
}
